__author__ = 'WinterHat'

from miniSysYVisitor import miniSysYVisitor
from miniSysYLexer import miniSysYLexer
from miniSysYParser import miniSysYParser
from tool import *


class HatVisitor(miniSysYVisitor):
    def __init__(self):
        memory = {}

    def visitFuncDef(self, ctx: miniSysYParser.FuncDefContext):
        type = self.visitFuncType(ctx.funcType())
        ident = self.visitIdent(ctx.ident())
        str = "defind %s %s" % (type, ident)
        print(str, end="")
        self.visitBlock(ctx.block())

    def visitFuncType(self, ctx: miniSysYParser.FuncTypeContext):
        return "dso_local i32"

    def visitBlock(self, ctx: miniSysYParser.BlockContext):
        stmt = self.visitStmt(ctx.stmt())
        str = "{\n\t%s\n}" % (stmt)
        print(str, end="")

    def visitIdent(self, ctx: miniSysYParser.IdentContext):
        return "@main"

    def visitStmt(self, ctx: miniSysYParser.StmtContext):
        return "ret " + toDecimal(ctx.Number().getText())

    def visitCompUnit(self, ctx: miniSysYParser.CompUnitContext):
        self.visitFuncDef(ctx.funcDef())
